package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.BizConsumer;
import com.situation.service.BizConsumerService;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BizConsumer)表控制层
 *
 * @author makejava
 * @since 2021-12-06 19:39:25
 */
@RestController
@RequestMapping("/api/consumer")
public class BizConsumerController {
    /**
     * 服务对象
     */
    @Autowired
    private BizConsumerService bizConsumerService;

    /**
     * 添加
     *
     * @param consumer 对象
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加物资去向失败",operation = "添加物资去向")
    @PostMapping("/add")
    @RequiresPermissions({"consumer:add"})
    public RespUtil add(BizConsumer consumer) {
        BizConsumer bizConsumer = bizConsumerService.insert(consumer);
        if (bizConsumer != null) {
            return RespUtil.ok().setData(consumer);
        }
        return RespUtil.fail();
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除物资去向失败",operation = "删除物资去向")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions({"consumer:delete"})
    public RespUtil delete(@PathVariable("id") Long id) {
        boolean b = bizConsumerService.deleteById(id);
        if (b)
            return RespUtil.ok();
        return RespUtil.fail();
    }

    /**
     * 查询单个
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"consumer:edit"})
    public RespUtil edit(@PathVariable("id") Long id) {
        BizConsumer bizConsumer = bizConsumerService.queryById(id);
        return RespUtil.ok().setData(bizConsumer);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public RespUtil findAll() {
        List<BizConsumer> bizConsumers = bizConsumerService.queryAll();
        return RespUtil.ok().setData(bizConsumers);
    }

    /**
     * 返回list对象
     *
     * @param pageNum  第几页
     * @param pageSize 每页显示条数
     * @param consumer 多条件查询对象
     * @return 对象列表
     */
    @GetMapping("/findConsumerList")
    public RespUtil findConsumerList(int pageNum, int pageSize, BizConsumer consumer) {
        PageInfo<BizConsumer> bizConsumerPageInfo = bizConsumerService.queryAllByCon(pageNum, pageSize, consumer);
        return RespUtil.ok().setData(bizConsumerPageInfo.getList());
    }

    /**
     * 修改
     *
     * @param consumer
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新物资去向失败",operation = "更新物资去向")
    @PutMapping("/update")
    @RequiresPermissions({"consumer:update"})
    public RespUtil update(BizConsumer consumer) {
        BizConsumer bizConsumer = bizConsumerService.update(consumer);
        if (bizConsumer != null) {
            return RespUtil.ok().setData(consumer);
        }
        return RespUtil.fail();
    }

}